﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;

namespace Telecom.Service
{
    public class RoleSer
    {
        private readonly EntitiesTelecom Context = new EntitiesTelecom();

        public int addOrupdate(ROLES entity)
        {
            try
            {
                if (entity.ROLEID == 0) Context.ROLES.Add(entity);
                else Context.Entry(entity).State = System.Data.EntityState.Modified;
                Context.SaveChanges();
                return 1;
            }
            catch { return 0; }
        }

        public void delete(int id)
        {
            try
            {
                var entity = Context.ROLES.SingleOrDefault(o => o.ROLEID == id);

                try
                {
                    var listRolesInUser = Context.USER_ROLES.Where(p => p.ROLEID == id);
                    var listRolesInMenu = Context.ROLE_MENUS.Where(p => p.ROLEID == id);

                    foreach (var item in listRolesInMenu)
                    {
                        Context.ROLE_MENUS.Remove(item);
                        Context.SaveChanges();
                    }

                    foreach (var item in listRolesInUser)
                    {
                        Context.USER_ROLES.Remove(item);
                        Context.SaveChanges();
                    }
                }
                catch { }

                Context.ROLES.Remove(entity);
                Context.SaveChanges();
            }
            catch { }
        }

        public void deleteByName(string Name)
        {
            try
            {
                var entity = Context.ROLES.SingleOrDefault(o => o.TEN == Name);
                Context.ROLES.Remove(entity);
                Context.SaveChanges();
            }
            catch { }
        }

        public string[] GetRolesForUser(string userName)
        {
            var c = (from i in Context.ROLES
                     join m in Context.USER_ROLES on i.ROLEID equals m.ROLEID
                     join l in Context.USERS on m.USERID equals l.USERID
                     where l.USERNAME == userName
                     select i).ToList();

            List<string> Temp = new List<string>();

            if (c.Count() > 0)
            {
                foreach (var g in c)
                {
                    Temp.Add(g.TEN);
                }
            }

            return Temp.ToArray();
        }

        public ROLES GetById(int id)
        {
            try
            {
                Context.Configuration.LazyLoadingEnabled = false;
                return Context.ROLES.SingleOrDefault(p => p.ROLEID == id);
            }
            catch { return null; }
        }

        public bool IsUserInRole(string username, string roleName)
        {
            try
            {
                var c = (from i in Context.ROLES
                         join m in Context.USER_ROLES on i.ROLEID equals m.ROLEID
                         join l in Context.USERS on m.USERID equals l.USERID
                         where l.USERNAME == username && i.TEN == roleName
                         select l);

                if (c != null) return true;
                else return false;
            }
            catch { return false; }
        }

        public List<ROLES> List()
        {
            try
            {
                Context.Configuration.LazyLoadingEnabled = false;
                return Context.ROLES.ToList();
            }
            catch { return null; }
        }

        public int Create_PhanQuyen(USER_ROLES entity)
        {
            try
            {
                Context.USER_ROLES.Add(entity);
                Context.SaveChanges();
                return 1;
            }
            catch { return 0; }
        }

        public int Update_PhanQuyen(USER_ROLES entity)
        {
            try
            {
                Context.Entry(entity).State = System.Data.EntityState.Modified;
                Context.SaveChanges();
                return 1;
            }
            catch { return 0; }
        }

        public USER_ROLES GetPhanquyenByUser(int userid)
        {
            try
            {
                var c = from i in Context.ROLES
                        join m in Context.USER_ROLES on i.ROLEID equals m.ROLEID
                        join l in Context.USERS on m.USERID equals l.USERID
                        where l.USERID == userid
                        select m;

                return c.First();
            }
            catch { return null; }
        }
    }
}
